﻿using cma.cimiss;
using cma.cimiss.client;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SXMeteorological.Data.Utility
{
   public class InterfaceUse
    {
      /// <summary>
      /// 查询数据返回数组
      /// </summary>
      /// <param name="InterfaceName">接口名</param>
      /// <param name="para">参数</param>
      /// <returns></returns>
       public RetArray2D QueryDataByInterface(string InterfaceName,Dictionary<string, string> para)
       {
           DataQueryClient client = new DataQueryClient();
           client.initResources();
           RetArray2D retArray2D = new RetArray2D();
           try
           {
               client.callAPI_to_array2D(CIMISSPara._uerID, CIMISSPara._passWord, InterfaceName, para, retArray2D);
           }
           catch (Exception ex)
           {

           }
           finally
           {
               client.destroyResources();
           }
           return retArray2D;
       }

       public string GetDatFileFullPath(RetArray2D retArray2D )
       {
           string path = string.Empty;
            path = AppDomain.CurrentDomain.BaseDirectory;
           //临时目录
           var tempDir = Path.Combine(path, "temp");
           if (!Directory.Exists(tempDir))
           {
               Directory.CreateDirectory(tempDir);
           }
           var guid = Guid.NewGuid().ToString();
           var currDir = Path.Combine(tempDir, guid);
           Directory.CreateDirectory(currDir);

           //散列点文件
           using (var dataStreamW = File.CreateText(Path.Combine(currDir, "data.dat")))
           {
               for (var i = 0; i < retArray2D.data.Length; i++)
               {
                   string[] s = retArray2D.data[i];

                   dataStreamW.WriteLine(string.Format("{0}\t{1}\t{2}", s[1], s[0], s[2]));
               }
               dataStreamW.Flush();
               dataStreamW.Close();
           };
           return path;
       }
    }
}
